//
//  ImageEditModel.m
//  TKnowBox
//
//  Created by LiuXubin on 15/6/26.
//  Copyright (c) 2015年 knowin. All rights reserved.
//

#import "SimImageEditModel.h"


#define kEditMarkSign @"-E_"

@interface SimImageEditModel ()
{
    ImageEditType _editType;
}

@end

@implementation SimImageEditModel

- (id)init
{
    self = [super init];
    if (self) {
        _editType = NSNotFound;
    }
    
    return self;
}

- (void)tryUpdateEditStatus
{
    if (_editType != IET_Origin) {
        NSString *orignalName = [[self class] orignalNameForEditName:_fileName];
        NSString *orignalPath = [_fileDir stringByAppendingPathComponent:orignalName];
        if ([[NSFileManager defaultManager] fileExistsAtPath:orignalPath] == NO) {
            [UIImageJPEGRepresentation(_orignalImage, 1.0) writeToFile:orignalPath atomically:YES];
        }
    }
}

- (void)cleanup
{
    NSString *originalFileName = [[self class] orignalNameForEditName:_fileName];
    [[self class] deleteImageName:originalFileName subpathsOfDirectoryAtPath:_fileDir];
}


- (UIImage *)orignalImage
{
    if (_orignalImage == nil) {
        NSString *orignalName = [[self class] orignalNameForEditName:_fileName];
        NSString *orignalPath = [_fileDir stringByAppendingPathComponent:orignalName];
        return [UIImage imageWithContentsOfFile:orignalPath];
    }
    
    return _orignalImage;
}

- (ImageEditType)editType
{
    if (_editType == NSNotFound) {
        _editType = [[self class] editTypeForImageName:_fileName];
    }
    
    return _editType;
}

- (void)setEditType:(ImageEditType)editType
{
    if (_editType != editType) {
        _editType = editType;
        _fileName = [[self class] editNameForEditType:editType name:_fileName];
    }
}

+ (NSString *)orignalNameForEditName:(NSString *)editName
{
    NSRange editRange = [editName rangeOfString:kEditMarkSign];
    if (editRange.location != NSNotFound) {
        return [editName substringToIndex:editRange.location];
    }
    
    return editName;
}

+ (NSString *)editNameForEditType:(ImageEditType)editType name:(NSString *)name
{
    NSString *orignalName = [self orignalNameForEditName:name];
    NSString *editName = nil;
    if (editType != IET_Origin) {
        editName = [orignalName stringByAppendingFormat:@"%@%ld", kEditMarkSign,(long)editType];
    }
    else{
        editName = orignalName;
    }
    
    return editName;
}

+ (ImageEditType)editTypeForImageName:(NSString *)imageName
{
    NSRange range = [imageName rangeOfString:kEditMarkSign];
    if (range.location != NSNotFound) {
        NSString *editTypeStr = [imageName substringFromIndex:NSMaxRange(range)];
        return [editTypeStr integerValue];
    }
    return IET_Origin;
}

+ (void)deleteImageName:(NSString *)imageName subpathsOfDirectoryAtPath:(NSString *)path
{
    NSFileManager *fm = [NSFileManager defaultManager];
    NSString *imagePath = [path stringByAppendingPathComponent:imageName];
 
    NSString *fileName = [imageName lastPathComponent];
    NSString *fileDir = [imagePath stringByDeletingLastPathComponent];
    NSArray *fileNames = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:fileDir error:nil];
    for (NSString *subName in fileNames) {
        if ([subName hasSuffix:fileName]) {
            NSString *filePath = [fileDir stringByAppendingPathComponent:subName];
            [fm removeItemAtPath:filePath error:nil];
        }
    }
}

@end
